package ru.bmstu.vks.client.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class FileDialog extends JDialog {

	private static final long serialVersionUID = 4338876858977448889L;
	
	private final JPanel contentPanel = new JPanel();
	private JFileChooser fileChooser;
	private boolean fileTransferAccepted = false;
	private File fileName;
	private JLabel sendFile;
	private JLabel cancel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			FileDialog dialog = new FileDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public FileDialog() {
		setModal(true);
		setTitle("\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u0444\u0430\u0439\u043B \u0434\u043B\u044F \u043F\u0435\u0440\u0435\u0434\u0430\u0447\u0438");
		setBounds(100, 100, 450, 400);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			fileChooser = new JFileChooser();
			contentPanel.add(fileChooser);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				{
					sendFile = new JLabel("");
					sendFile.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseClicked(MouseEvent e) {
							fileName = fileChooser.getSelectedFile();
							fileTransferAccepted = true;
							FileDialog.this.setVisible(false);
						}
					});
					sendFile.setIcon(new ImageIcon(FileDialog.class.getResource("/data/accept.png")));
					buttonPane.add(sendFile);
				}
			}
			{
				cancel = new JLabel("");
				cancel.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						fileName = null;
						fileTransferAccepted = false;
						FileDialog.this.setVisible(false);
					}
				});
				cancel.setIcon(new ImageIcon(FileDialog.class.getResource("/data/cancel.png")));
				buttonPane.add(cancel);
			}
		}
	}
	
	public File getSelectedFile() {
		return fileName;
	}
	
	public boolean getTransferAccepted() {
		return fileTransferAccepted;
	}

}
